<template>
	<!-- 库间移位  ， 组织间移位 ， 公司间移位  移出提交 -->
	<view class="">
		<!-- 主表信息 -->
		<view class="mian-table">
			<view class="main-table-title">
				<view class="mian-table-name">
					表头信息
				</view>
			</view>
			<view class="mian-table-content">
				<formItem :formConfig='filterMainCF' :formVal='formValueMain' @searchBaseP='searchBaseP'></formItem>
			</view>
		</view>
		<!-- 子表信息 -->
		<view class="sub-table">
			<view class="sub-table-title">
				<view class="sub-table-title-name">物资信息 </view>
				<view class="sub-table-title-contrl">
					<!-- <text class="dicon diconjia-xianxingfangkuang" @click="addMater"></text> -->
					<text class="dicon diconsaomiao" @click="ScanEvent"></text>
				</view>
			</view>

			<view class="sub-table-content" v-for="(item,index) in formValueSub " :key="index" @click="activeSub(index)">
				<formItem :formConfig="filterSubCF" :formVal="item" :formIndex="index" @searchBaseP='searchBaseP'></formItem>
			</view>
			
		</view>
		
		<view class="operation">
			<view class="operation-button border-right" @click="finishSubmit">
				提交
			</view>
		</view>
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import config from '@/common/config.js'
	import formItem from '@/components/form/form_item.vue'
	import api from '@/common/api.js'
	const plug = uni.requireNativePlugin('Html5app-Gprinter');
	let _self,valueSub , activeType ,activeID,activeSubIndex , ResultData={} ,FRBILLNO,submitType ;
	export default {
		data() {
			return {
				config: [],
				bstypy: "" ,
				configType:'', // 配置类型
				formValueMain: {},
				formValueSub:[]
			}
		},
		computed: {
			...mapState(['userInfo','checkData']),
			filterMainCF() {
				// let aa = this.bsType+'MainFormCF'  ;
				return this.config[this.configType].filter(item => {
					return (item.IsShow == 1 && item.GroupID==1);
				})
			},
			filterSubCF() {
				// let  bb = this.bsType+'SubformCF' ;
				return this.config[this.configType].filter(item => {
					return (item.IsShow == 1 && item.GroupID==2);
				})
			}
		},
		components: {
			formItem
		},
		onLoad(p) {
			_self = this
			this.setCheckData([])
			this.formValueMain = JSON.parse( decodeURIComponent(p.data) )
			this.config = config.App
			this.bstypy = p.type
			FRBILLNO = p.id
			// this.initConifg(p.type)
			this.listenOnce()
			this.configType = p.busType +'OutCF'
			let titles = {1:'库间移出',2:'公司间移出',3:'组织间移出'}
			submitType = p.typeKey
			// console.log(this.configType);
			console.log(this.formValueMain);
			console.log(submitType);
			this.getMaters()
			this.$utils.setPageTitle(titles[p.typeKey])
		},
		onShow() {
			// if(this.checkData.length>0){
			// 	this.formValueSub = this.checkData 
			// }
		},
		methods: {
			...mapMutations(["setCheckData"]),
			// 初始化配置数据
			initConifg(bsType) {
				// this.formValueSub.push(t)
				// console.log(valueMain);
				// console.log(valueSub);
			},
			// 激活的第几个子表
			activeSub(i){
				activeSubIndex = i ;
				console.log(activeSubIndex);
			},
			
			
			initForm(config){
				let valueMain = {};
				config.forEach(item => {
					valueMain[item.ID] = valueMain[item.Default] || ''
				})
				return valueMain;
			},
			// 扫描物资
			ScanEvent(){
				 // console.log(this.formValueMain);
				 let URL =  api.apiPublic.ScanCode;
				 let Arrays =  this.checkData ;
				 this.$utils.Scan(URL, item => {
					 item.INSTATIONNAME = ''
					 item.PK_INSTATIONNAME = ''
					 item.OUTPHOTO = ''
						Arrays.push(item)
						_self.setCheckData(Arrays)
						// _self.formValueSub = _self.checkData
				 });
			},
			// 基础数据查询
			searchBaseP(tnt){
				activeType = tnt.type
				activeID = tnt.id
				let queryParames = "" , search_PK_GROUP = '';
				console.log(tnt);
				if (activeType == "BD_RACK" && !_self.formValueMain.STORENAME) {
					return _self.$utils.tips("请先选择仓库");
				} else if (activeType == "BD_RACK") {
					queryParames = JSON.stringify({
						PK_STORE: this.formValueMain.PK_STORE
					});
				}else if (activeType == "BA_ORGANIZE") {
					search_PK_GROUP = _self.formValueMain.PK_GROUP
				}else if(activeType == "BD_STORE"){
					search_PK_GROUP = _self.formValueMain.PK_ORG
				}
				
				let URL  = "/pages/public/search?t=" + activeType + "&queryjson=" + queryParames+'&PK_GROUP='+search_PK_GROUP
				uni.navigateTo({
					url: URL
				});
				
			},
			
			
			// 初始监听
			listenOnce(){
				uni.$on("getSelectItem", function(data) {
					console.log(data);
					switch (activeType) {
						case "GROUP":
							_self.formValueMain.GROUPNAME = data.FullName;
							_self.formValueMain.PK_GROUP = data.OrganizeId;
							break;
						case "BA_ORGANIZE":
							_self.formValueMain.ORGNAME = data.FullName;
							_self.formValueMain.PK_ORG = data.OrganizeId;
							break;
						case "BA_DEPARTMENT":
							_self.formValueMain.DEPTNAME = data.FullName;
							_self.formValueMain.PK_DEPT = data.OrganizeId;
							break;
						case "BD_SUPPLIER":
							_self.formValueMain.SUPPLYNAME = data.VNAME;
							_self.formValueMain.PK_SUPPLY = data.BILLNO;
							break;
						case "BA_USER":
							_self.formValueMain.APPLYER = data.RealName;
							_self.formValueMain.PK_APPLYER = data.UserId;
							break;
						case "BD_STORE": // 仓库
								console.log(activeType,activeID);
								if(activeID=='INSTORENAME'){
									_self.formValueMain.PK_INSTORE = data.BILLNO;
									_self.formValueMain.INSTORENAME = data.VNAME;
								}else {
									_self.formValueMain.PK_STORE = data.BILLNO;
									_self.formValueMain.STORENAME = data.VNAME;
								}
								
							break;
						case "BD_RACK": // 货位
								// _self.formValueSub[activeSubIndex].INSTATIONNAME = data.FULLVNAME;
								// _self.formValueSub[activeSubIndex].PK_INSTATIONNAME = data.BILLNO;
								_self.checkData[activeSubIndex].INSTATIONNAME = data.FULLVNAME;
								_self.checkData[activeSubIndex].PK_INSTATIONNAME = data.BILLNO;
							break;
						default:
							break;
					}
				});
			},
			
			getMaters(){
					let query = {
						"category": "ST_MOVEAPPLY",
						"page": 1,
						"rows": "500",
						"sidx": "",
						"sord": "",
						"queryJson": JSON.stringify({PK_MOVEAPPLY:FRBILLNO}),
						"userId": _self.userInfo.UserId,
						"account": _self.userInfo.Account
					}
					console.log(query);
					let URL = this.userInfo.BaseURL + api.apiPublic.getMaterList;
					this.axios({
						url: URL,
						data: query,
						isLoading:1
					}).then(res=>{
						let {
							IsSuccess,
							Message,
							Result
						} = res;
						let arr = Result.rows.map(item=>{
							item.is_check = false
							return item ;
						})
						_self.formValueSub = arr
						// console.log(res);
					})
			},
			
			
			
			
			
			finishSubmit(){
				
				for(var i=0;i< this.formValueSub.length;i++){
					let item =  this.formValueSub[i];
					if(!item.is_check){
						return this.$utils.tips('物资未扫码校验不可出库')
					}else if(!item.NNUM){
						return this.$utils.tips('移出数量不能为空')
					}
				}
				
				let savedata = {
					..._self.formValueMain,
					"BILLFROM":0,
					"BTCATEGORY": submitType ,
					"MOVETYPE":0, // 0 移出  1 移入
					"FRBILLNO": _self.formValueMain.BILLNO,
					"BILLMAKER": _self.formValueMain.BILLMAKER,
					"userId": _self.userInfo.UserId,
					"account": _self.userInfo.Account,
					"Detail": JSON.stringify(_self.formValueSub)
				}
				console.log(savedata);
				let URL =  api.apiPublic.DisplacementAdd;
				// console.log(URL);
				this.axios({
					url: URL,
					data: savedata,
					isLoading: 1
				}).then(res => {
					let {
						Message,
						Result
					} = res;
					// // console.log(Result);
					// ResultData = Result
					_self.$utils.tips('提交成功')
					setTimeout(a=>{
						uni.navigateBack({})
					},1500)
				})
			},
			
			// 去打印
			gotoPrint(t){
				// console.log('pp'+t);
				// console.log(ResultData);
				uni.navigateTo({
					url:'print_list?type='+t+'&data='+encodeURIComponent(JSON.stringify(ResultData))
				})
			}
			
			

		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 110rpx;
	}

	.mian-table {
		background-color: #ffffff;
		color: #555555;

		.main-table-title {
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;

			.mian-table-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.mian-table-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}
		}

		.mian-table-content {
			padding: 0 30rpx;
		}
	}

	.sub-table {
		color: #555555;

		.sub-table-title {
			background-color: #ffffff;
			display: flex;
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;
			justify-content: space-between;
			align-items: center;

			.sub-table-title-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.sub-table-title-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}

			.dicon {
				font-size: 50rpx;
				margin: 0 10rpx;
			}
		}

		.sub-table-content {
			margin-top: 20rpx;
			background-color: #ffffff;
			padding: 0 30rpx;
		}
	}
</style>
